package com.longshine.util;

import java.io.File;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class CommonUtil
{
  public static Map<String, String> str2Map(String src)
  {
    String[] styles = src.split(";");
    Map m = new HashMap();
    for (String style : styles) {
      String[] kv = style.split(":");
      m.put(kv[0], kv[1]);
    }
    return m;
  }
  public static String map2Str(Map<String, String> map) {
    String rt = "";
    for (Iterator localIterator = map.entrySet().iterator(); localIterator.hasNext(); ) { Object o = localIterator.next();
      String k = ((Map.Entry)o).getKey().toString();
      String v = ((Map.Entry)o).getValue().toString();
      rt = rt + k + ":" + v + ";";
    }
    rt = rt.substring(0, rt.length() - 1);
    return rt;
  }

  public static String getSvgUrl(String fileName)
  {
    File f = new File(fileName);
    try {
      return f.toURL().toString();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
    return null;
  }

  public static void delay(long ms)
  {
    if (ms <= 0L)
      return;
    try
    {
      Thread.sleep(ms);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  public static String formatState(int state) {
    return String.format("%02d", new Object[] { Integer.valueOf(state) });
  }

  public static int[] list2IntArr(List<Integer> l) {
    if (l == null) return null;
    int[] ret = new int[l.size()];
    for (int i = 0; i < l.size(); i++) {
      ret[i] = ((Integer)l.get(i)).intValue();
    }
    return ret;
  }

  public static int getHashValue(int sysType, int downNo)
  {
    return (sysType << 4) + downNo;
  }
  public static int getSysTypeByHashValue(int value) {
    return value >> 4;
  }
  public static int getDownNoTypeByHashValue(int value) {
    return value % 16;
  }

  public static String getTodayValue() {
    Date d = new Date();
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    String s = sf.format(d);
    return s;
  }
  public static String getCurrentTime() {
    Date d = new Date();
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
    String s = sf.format(d);
    return s;
  }

  public static Date getCurrentDate() {
    return new Date();
  }

  public static String formatTime(Date date) {
    if (date == null) return null;
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String s = sf.format(date);
    return s;
  }

  public static void main(String[] args)
  {
    System.out.println(toHexStr(10));
  }

  public static int binary2Decimal(String s) {
    char[] cs = s.toCharArray();
    int ret = 0;
    for (int i = 0; i < cs.length; i++) {
      ret = (int)(ret + Math.pow(2.0D, cs.length - i - 1) * (cs[i] == '1' ? 1 : 0));
    }
    return ret;
  }

  public static int toHex(String num)
  {
    int ret = 0;
    num = num.toLowerCase();
    char[] chars = num.toCharArray();
    for (int i = 0; i < chars.length; i++) {
      char c = chars[i];
      ret <<= 4;
      ret += (c >= 'a' ? '\n' + c - 97 : c - '0');
    }
    return ret;
  }

  public static String toHexStr(int num)
  {
    int t = num;
    String ret = "";
    int devidedNum = t / 16;
    int leftNum = t % 16;
    ret = ret + toHexChar(devidedNum);
    ret = ret + toHexChar(leftNum);
    return ret;
  }

  private static char toHexChar(int num)
  {
    char ret = '0';
    if (num < 10) {
      ret = (char)(48 + num);
    }
    else {
      ret = (char)(97 + (num - 10));
    }
    return ret;
  }

  public static Integer[] intArr2IntegerArr(int[] intArr) {
    Integer[] ret = new Integer[intArr.length];
    for (int i = 0; i < ret.length; i++) {
      ret[i] = Integer.valueOf(intArr[i]);
    }
    return ret;
  }

  public static LinkedList arr2List(int[] intarr) {
    LinkedList l = new LinkedList();
    for (int i : intarr) {
      l.add(Integer.valueOf(i));
    }
    return l;
  }

  public static Object[] list2Arr(List l) {
    Object[] os = new Object[l.size()];
    return l.toArray(os);
  }

  public static String getUrl(File f)
  {
    try
    {
      return f.toURL().toString();
    }
    catch (MalformedURLException e) {
      e.printStackTrace();
    }return null;
  }
}